﻿using System.Linq;
using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using NPOI.HSSF.Record;
using System.Threading.Tasks;
using System.Threading;
using Microsoft.EntityFrameworkCore;
using TyMES.MidWare;
using TyMES.Model.BaseInformation;
using WalkingTec.Mvvm.Core;
using System;

namespace TyMES.Services
{
    
    public class DataMigrationBackgroundService : BackgroundService
    {
        private readonly HttpService httpService;
        private readonly IConfiguration configuration;
        private readonly ILogger logger;
        private readonly IServiceScope serviceScope;
        private readonly WTMContext context;
        private OrderService _orderService;
        private string _LineCode;
        //private readonly IHubContext<NotificationHub> notificationHub;


        public DataMigrationBackgroundService(IConfiguration configuration, ILogger<DataMigrationBackgroundService> logger, IServiceScopeFactory serviceScopeFactory)
        {
            this.configuration = configuration;
            this.serviceScope = serviceScopeFactory.CreateScope();
            var sp = this.serviceScope.ServiceProvider;
            this.logger = logger;
            this.context = sp.GetRequiredService<WTMContext>();
            this.httpService = sp.GetRequiredService<HttpService>();
            _orderService = sp.GetRequiredService<OrderService>();
            this._LineCode = configuration["LineCode"];
        }

        // 后台需要上传的数据：设备状态、不良品登记
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            while (!stoppingToken.IsCancellationRequested)
            {
                //await _orderService.UpdateOrderAsync(_LineCode);
                //await Console.Out.WriteLineAsync("-----");
                await Task.Delay(10000);

            }
        }


        public override void Dispose()
        {
            base.Dispose();
            serviceScope.Dispose();
        }
    }
   
}
